﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Speech.Recognition;

namespace Music_Application
{
    class Recognition
    {
        SpeechRecognitionEngine _recognize;
        MediaForm form;

        public Recognition(Object ObjectName)
        {
            _recognize = new SpeechRecognitionEngine();
            LoadCommand();
            _recognize.SpeechRecognized += _recognize_SpeechRecognized;
            _recognize.SetInputToDefaultAudioDevice();
            _recognize.RecognizeAsync(System.Speech.Recognition.RecognizeMode.Multiple);
            form = (MediaForm)ObjectName;
        }

        ~Recognition()
        {
            _recognize.Dispose();
        }
        void _recognize_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            switch (e.Result.Text)
            {
                case "ADD MUSIC LIST":
                    form.AddMusicList();
                    break;
                case "SHOW MUSIC LIST":
                    form.ShowMusicList();
                    break;
                case "PLAY MUSIC":
                    form.PlayMusic();
                    break;
                case "PAUSE MUSIC":
                    form.PauseMusic();
                    break;
                case "SET AUTOPLAY":
                    form.SetStateMediaAutoPlay("AUTOPLAY");
                    break;
                case "STOP AUTOPLAY":
                    form.SetStateMediaAutoPlay("STOP AUTOPLAY");
                    break;
                case "SET AUTOPLAY ONE SONG":
                    form.SetStateMediaAutoPlay("AUTOPLAY ONE SONG");
                    break;
                default:
                    if (e.Result.Text.IndexOf("SET VOLUME") == 0)
                    {
                        int numbersong = Convert.ToInt32(e.Result.Text.Substring(("SET VOLUME").Length).Trim());
                        form.SetVolume(numbersong);
                    }
                    else if (e.Result.Text.IndexOf("PLAY NUMBER") == 0)
                    {
                        int sokitu = e.Result.Text.Length - ("PLAY NUMBER").Length - ("SONG").Length;
                        int numbersong = Convert.ToInt32(e.Result.Text.Substring(("PLAY NUMBER").Length, sokitu).Trim()) - 1;//table bat dau tu hang so 0
                        form.PlaySongNumber(numbersong);
                    }
                    break;
            }
        }

        private void LoadCommand()
        {
            if (_recognize != null)
            {
                _recognize.LoadGrammarAsync(new Grammar(new GrammarBuilder("ADD MUSIC LIST")));
                _recognize.LoadGrammarAsync(new Grammar(new GrammarBuilder("SHOW MUSIC LIST")));
                _recognize.LoadGrammarAsync(new Grammar(new GrammarBuilder("PLAY MUSIC")));
                _recognize.LoadGrammarAsync(new Grammar(new GrammarBuilder("PAUSE MUSIC")));
                _recognize.LoadGrammarAsync(new Grammar(new GrammarBuilder("SET AUTOPLAY")));
                _recognize.LoadGrammarAsync(new Grammar(new GrammarBuilder("STOP AUTOPLAY")));
                _recognize.LoadGrammarAsync(new Grammar(new GrammarBuilder("SET AUTOPLAY ONE SONG")));
                Choices choices = new Choices();
                for (int i = 0; i <= 100; i++)
                {
                    choices.Add(i.ToString());
                }
                GrammarBuilder builder = new GrammarBuilder();
                builder.Append("SET VOLUME");
                builder.Append(choices);
                _recognize.LoadGrammarAsync(new Grammar(builder));

                builder = new GrammarBuilder();
                builder.Append("PLAY NUMBER");
                builder.Append(choices);
                builder.Append("SONG");
                _recognize.LoadGrammarAsync(new Grammar(builder));
            }
        }
    }
}
